//
//  SetView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI
import Kingfisher




struct AiSetView: View {
    
    enum FromType : Int , Codable {
        case FROM_DEFAULT = 0
        case FROM_AI = 1
        case  FROM_VOICE = 2
        case FROM_CHAT = 3
        case FROM_ACT = 4
        case FROM_INSTRUCTION = 5
        case FROM_MEMORY = 6
        case FROM_DIARY = 7
    }
    
    var from : FromType
    @EnvironmentObject var themeManager: ThemeManager
    @StateObject var viewModel = AiSetViewModel()
    @State var delTips = false
    
    
    var body: some View {
        ZStack{
            ScrollView(.vertical) {
                if (from == FromType.FROM_DEFAULT || from == FromType.FROM_AI){
                    AiSetCommonView(forceShow: from == FromType.FROM_AI)
                    Spacer(minLength: 15)
                }
                if (from == FromType.FROM_DEFAULT || from == FromType.FROM_VOICE){
                    AiSetVoiceView(forceShow: from == FromType.FROM_VOICE, viewModel: viewModel)
                    Spacer(minLength: 15)
                }
                if (from == FromType.FROM_DEFAULT || from == FromType.FROM_CHAT) {
                    AiChatView(forceShow: from == FromType.FROM_CHAT)
                    Spacer(minLength: 15)
                }
                if (from == FromType.FROM_DEFAULT || from == FromType.FROM_ACT){
                    AiActView(forceShow: from == FromType.FROM_ACT)
                    Spacer(minLength: 15)
                }
                if (from == FromType.FROM_DEFAULT || from == FromType.FROM_MEMORY){
                    AiSetMemoryView(forceShow: from == FromType.FROM_MEMORY)
                    Spacer(minLength: 15)
                }
                if (from == FromType.FROM_DEFAULT || from == FromType.FROM_DIARY){
                    AiSetDiary(forceShow: from == FromType.FROM_DIARY)
                    Spacer(minLength: 15)
                }
                if (from == FromType.FROM_DEFAULT){
                    if viewModel.isLoading {
                        // 显示加载指示器，比如一个 ProgressView
                        ProgressView()
                            .progressViewStyle(CircularProgressViewStyle())
                            .padding().onAppear() {
                                viewModel.getSystemAiRoleList()
                            }
                    } else {
                        VStack(alignment:.leading){
                            if viewModel.isAiVisible { //目的是为了控制动画
                                VStack(alignment:.leading){
                                    HStack{
                                        Text("选择AI伙伴").font(.system(size: 16)).foregroundColor(.bgNormalText2)
                                        Image(.appIconAdd)
                                            .resizable()
                                            .scaledToFill()
                                            .padding(EdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5))
                                            .background(themeManager.themeColor)
                                            .clipShape(Circle())
                                            .frame(width: 20,height: 20)
                                            .onTapGesture {
                                                pathManager.jump(type: JumpType.AI_ROLE)
                                            }
                                        Spacer()
                                    }
                                    Text("支持多选").font(.system(size: 12)).foregroundColor(.bgNormalText3)
                                }.frame(maxWidth: .infinity).padding()
                                // 显示数据列表
                                ForEach(viewModel.items,id: \.objectId) { item in
                                    AiRoleItemView(bean: item, items: $viewModel.items)
                                }
                            }
                        }.frame(maxWidth: .infinity).background(.bgItem).cornerRadius(15).onAppear(){
                            withAnimation(Animation.easeInOut(duration: 0.3)) {
                                viewModel.isAiVisible = true
                            }
                        }
                        
                    }
                    Spacer(minLength: 15)
                }
            }.navigationTitle(getTitle())
                .frame(maxWidth: .infinity)
                .padding().background(.bg)
                .toolbar {
                    // 添加一个右标题按钮
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Text("购买次数")
                            .foregroundColor(themeManager.themeColor)
                            .font(.system(size: 12))
                            .padding(.horizontal,10)
                            .padding(.vertical,7)
                            .cornerRadius(8)
                            .contentShape(Rectangle())
                            .onTapGesture {
                                pathManager.jump(type: JumpType.BUY_COUNT)
                            }
                    }
                }
        }.onAppear{
            showLog("来源：\(from)")
        }.onDisappear{
            viewModel.cancelRequest()
        }
    }
    
    func getTitle()->String {
        switch from {
        case .FROM_DEFAULT:return "Ai生活记录伴侣"
        case .FROM_AI:
            return "AI设置"
        case .FROM_VOICE:
            return "语音设置"
        case .FROM_CHAT:
            return "聊天设置"
        case .FROM_ACT:
            return "活动"
        case .FROM_INSTRUCTION:
            return "指令玩法"
        case .FROM_MEMORY:
            return "记忆库"
        case .FROM_DIARY:
            return "日记设置"
        }
    }
}

//#Preview {
//    SetView()
//}


//定义预览提供者
struct AiSet_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: AiSetView(from: AiSetView.FromType.FROM_DEFAULT))
    }
}
